<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('查看')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="620px" top='5vh' class="StrategyCheckDialog" @close='close'>
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" label-width="100px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('名称')" prop="name">
          <el-input v-emoji v-model="record.name" ref="recordName" disabled style="width:217px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('类型')" prop="type">
          <span v-if="record.type === 'single'">{{$t('单出口模式')}}</span>
          <span v-else>{{$t('多出口模式')}}</span>
        </el-form-item>
        <el-form-item label="IPv4/IPv6" prop="ip">
          <el-radio v-model="record.ip" class="ipv4" disabled label="ipv4">IPv4</el-radio>
          <el-radio v-model="record.ip" class="ipv6" disabled label="ipv6">IPv6</el-radio>
        </el-form-item>
        <el-form-item :label="$t('负载类型')" prop="balance" v-if="record.type === 'multi'">
          <el-select v-model="record.balance" ref="balance" :placeholder="$t('请选择')" size='small' disabled filterable clearable>
            <el-option v-for="(item, key) in balanceList" :key="key" :label="item" :value="key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('状态')" prop="status">
          <el-radio v-model="record.status" class="able" disabled label="able">{{$t('开启')}}</el-radio>
          <el-radio v-model="record.status" class="disables" disabled label="disable">{{$t('关闭')}}</el-radio>
        </el-form-item>
        <el-form-item :label="$t('出接口')" prop="dev" v-if="record.type === 'multi'">
          <el-select v-model="record.dev" ref="recordDev" :placeholder="$t('请选择出接口')" multiple clearable filterable disabled size='small'>
            <el-option v-for="item in physicalList" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('出接口')" prop="dev" v-if="record.type === 'single'">
          <el-select v-model="record.dev" class="recordDev" :placeholder="$t('请选择出接口')" disabled clearable filterable size='small'>
            <el-option v-for="item in physicalList" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('下一跳')" prop="gw" v-if="record.type === 'single'">
          <el-input v-emoji v-model="record.gw" ref="recordGw" style="width:217px;" disabled :placeholder="$t('若出接口存在默认网关可不填')" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('源IP')" prop="src">
          <el-select v-model="record.src" :placeholder="$t('请选择')" ref="recordSrc" size='small' disabled filterable clearable>
            <el-option v-for="item in srcList" :key="item.id" :label="item.nameShow" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('目的IP')" prop="dst">
          <el-select v-model="record.dst" :placeholder="$t('请选择')" ref='recordDst' size='small' disabled filterable clearable>
            <el-option v-for="item in dstList" :key="item.id" :label="item.nameShow" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('服务')" prop="ser">
          <el-select v-model="record.ser" :placeholder="$t('请选择')" ref="recordSer" size='small' disabled filterable clearable>
            <el-option v-for="item in iservicepsList" :key="item.id" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('应用')" prop="app">
          <el-select v-model="record.app" :placeholder="$t('请选择')" ref='recordApp' size='small' disabled filterable clearable>
            <el-option v-for="item in appList" :key="item.id" :label="item.name" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('描述')" prop="desc">
          <el-input v-emoji v-model="record.desc" ref="recordDesc" style="width:400px;" disabled size='small' :autosize="{ minRows: 4 }" type="textarea" @keydown.native="handleKeyCode($event)"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close" type="primary" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { getAddrGroupArray } from '@/api/list/addr-group'
import { getAddrArray } from '@/api/list/addr'
import { getServiceArray, getServiceReArray } from '@/api/list/server'
import { getAppArray, getAppReArray } from '@/api/list/app'
import { inputClass, selectClass, texttextClass } from '@/utils/input-class.js'
export default {
  name: 'StrategyCheckDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('请输入名称')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: null,
        name: '',
        type: 'single',
        ip: 'ipv4',
        status: 'able',
        dev: '',
        gw: '',
        src: '',
        dst: '',
        ser: '',
        app: '',
        desc: '',
        balance: ''
      },
      recordCopy: {
        id: null,
        name: '',
        type: 'single',
        ip: 'ipv4',
        status: 'able',
        dev: '',
        gw: '',
        src: '',
        dst: '',
        ser: '',
        app: '',
        desc: '',
        balance: ''
      },
      balanceList: {
        'round': this.$t('轮询负载'),
        'width': this.$t('带宽负载'),
        'super': this.$t('最优路径')
      },
      physicalList: [],
      srcList: [],
      dstList: [],
      iservicepsList: [],
      appList: [],
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  watch: {
    'record.desc': {
      handler (newVal) {
        this.record.desc = newVal.replace(/[\r\n]/g, '')
      },
      immediate: true
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordName', 'recordGw']
        const selectData = ['balance', 'recordSrc', 'recordSer', 'recordDst', 'recordApp']
        const textdata = ['recordDesc']
        texttextClass(this.$refs, textdata)
        selectClass(this.$refs, selectData)
        inputClass(this.$refs, data)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
        this.record.status = data.state
        if (this.record.type === 'single') {
          this.record.gw = data['mutil-dev'][0].gw
          this.record.dev = data['mutil-dev'][0].dev
        } else {
          this.record.dev = []
          for (let item of data['mutil-dev']) {
            this.record.dev.push(item.dev)
          }
        }
      }
      this.physicalApiArray()
      this.commandList()
      this.serviceList()
      this.appDataList()
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.physicalList = []
      this.srcList = []
      this.dstList = []
      this.iservicepsList = []
      this.appList = []
      this.$refs.form.resetFields()
    },
    physicalApiArray () {
      this.physicalList = []
      getPhysicalApiArray().then(response => {
        if (response.status === 0) {
          this.physicalList = response.info
        }
      })
    },
    commandList (data, nameType, name) {
      this.srcList = []
      this.dstList = []
      getAddrArray().then(response => {
        if (response.status === 0) {
          getAddrGroupArray().then(res => {
            if (res.status === 0) {
              // this.srcList = response.info.concat(res.info)
              // this.dstList = response.info.concat(res.info)
              for (let item of response.info) {
                item.nameShow = this.$t('地址-') + item.name
                this.srcList.push(item)
                this.dstList.push(item)
              }
              for (let item of res.info) {
                item.nameShow = this.$t('地址组-') + item.name
                this.srcList.push(item)
                this.dstList.push(item)
              }
            }
          })
        }
      })
      if (data && nameType === 'src') {
        this.record.src = name
      } else if (data && nameType === 'dst') {
        this.record.dst = name
      }
    },
    serviceList (data) {
      this.iservicepsList = []
      getServiceArray().then(response => {
        if (response.status === 0) {
          getServiceReArray().then(res => {
            if (res.status === 0) {
              this.iservicepsList = response.info.concat(res.info)
            }
          })
        }
      })
      if (data) {
        this.record.ser = data.name
      }
    },
    appDataList () {
      this.appList = []
      getAppArray().then(response => {
        if (response.status === 0) {
          getAppReArray().then(res => {
            if (res.status === 0) {
              this.appList = res.info.concat(response.info)
            }
          })
        }
      })
    },
    handleKeyCode (event) {
      if (event.keyCode === 13) {
        event.preventDefault() // 阻止浏览器默认换行操作
        return false
      }
    }
  }
}
</script>
<style lang="less">
.StrategyCheckDialog {
  .el-input.is-disabled .el-input__inner {
    background-color: #fff !important;
    color: #606266 !important;
  }

  .el-textarea.is-disabled .el-textarea__inner {
    background-color: #fff !important;
    color: #606266 !important;
  }
}
</style>
<style scoped>
.dialogContentBox {
  max-height: calc(90vh - 184px);
  overflow-y: auto;
}
</style>
